#include<bits/stdc++.h>
using namespace std;
int p[200006];

int solve() {
  int n;cin>>n;
  for(int i=0;i<n;++i)cin>>p[i];
  sort(p,p+n);

  double median;
  if(n%2) { //odd
    median = p[n/2];
  }
  else median = ((double)p[n/2-1] + (double)p[n/2]) / 2 ;

  double ans=0;
  for(int i=0;i<n;++i) {
    ans += abs(p[i]-median);
  }

  cout << (long long)ans << "\n";
  return 0;
}
int main() {
  solve();
  return 0;
}
